package com.esri.viewer.components.preloader
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.events.FlexEvent;
	import mx.preloaders.IPreloaderDisplay;
	
	public class CustomPreloader extends Sprite implements IPreloaderDisplay
	{
		// Define a Loader control to load the SWF file.
		private var dpbImageControl:flash.display.Loader;
		
		public function CustomPreloader() {   
			super();        
		}
		
		// Specify the event listeners.
		public function set preloader(preloader:Sprite):void {
			// Listen for the relevant events
			preloader.addEventListener(
				ProgressEvent.PROGRESS, handleProgress); 
			preloader.addEventListener(
				Event.COMPLETE, handleComplete);
			
			preloader.addEventListener(
				FlexEvent.INIT_PROGRESS, handleInitProgress);
			preloader.addEventListener(
				FlexEvent.INIT_COMPLETE, handleInitComplete);
		}
		
		// Initialize the Loader control in the override 
		// of IPreloaderDisplay.initialize().
		public function initialize():void {
			dpbImageControl = new flash.display.Loader();       
			dpbImageControl.contentLoaderInfo.addEventListener(
				Event.COMPLETE, loader_completeHandler);
			dpbImageControl.load(new URLRequest("assets/images/preloader.swf"));         
		}
		
		// After the SWF file loads, set the size of the Loader control.
		private function loader_completeHandler(event:Event):void
		{
			addChild(dpbImageControl);
			dpbImageControl.width = 400;
			dpbImageControl.height= 400;
			dpbImageControl.x = 500;
			dpbImageControl.y = 200;
		}   
		
		// Define empty event listeners.
		private function handleProgress(event:ProgressEvent):void {
		}
		
		private function handleComplete(event:Event):void {
		}
		
		private function handleInitProgress(event:Event):void {
		}
		
		private function handleInitComplete(event:Event):void {
			var timer:Timer = new Timer(2000,1);
			timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
			timer.start();      
		}
		
		private function dispatchComplete(event:TimerEvent):void {
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		// Implement IPreloaderDisplay interface
		
		public function get backgroundColor():uint {
			return 0;
		}
		
		public function set backgroundColor(value:uint):void {
		}
		
		public function get backgroundAlpha():Number {
			return 0;
		}
		
		public function set backgroundAlpha(value:Number):void {
		}
		
		public function get backgroundImage():Object {
			return undefined;
		}
		
		public function set backgroundImage(value:Object):void {
		}
		
		public function get backgroundSize():String {
			return "";
		}
		
		public function set backgroundSize(value:String):void {
		}
		
		public function get stageWidth():Number {
			return 200;
		}
		
		public function set stageWidth(value:Number):void {
		}
		
		public function get stageHeight():Number {
			return 200;
		}
		
		public function set stageHeight(value:Number):void {
		}	}
}